<template>
  <div class="teacher-page">
    <el-card>
    <div class="header-title">教师管理</div>
    <div class="teacher-header">
      <el-input v-model="sname" placeholder="教师姓名/联系电话/职称/课程" class="search-input" />
      <el-button type="primary" @click="handleSearch">查询</el-button>
      <el-button @click="handleReset">重置</el-button>
      <el-button type="primary" class="add-btn" @click="goAdd">+ 新增教师</el-button>
    </div>
    <el-alert :title="`查询结果：共计 ${total} 条数据`" type="info" show-icon class="result-alert" />
    <el-table :data="tableData" border stripe class="teacher-table">
      <el-table-column prop="name" label="教师姓名" width="120" />
      <el-table-column prop="coursesTaught" label="所授课程" width="180" />
      <el-table-column prop="professionalTitle" label="职称" width="120" />
      <el-table-column prop="contactPhone" label="联系电话" width="140" />
      <el-table-column label="操作" width="120">
        <template #default="scope">
          <el-link type="primary" @click="goEdit(scope.row)">编辑</el-link>
          <el-link type="danger" @click="handleDelete(scope.row.id)">删除</el-link>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <el-pagination
        v-model:current-page="current"
        v-model:page-size="size"
        :total="total"
        :page-sizes="[10, 20, 50, 100]"
        layout="total, sizes, prev, pager, next"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { ElMessage, ElMessageBox } from 'element-plus';
import { getTeacherList, addTeacher, updateTeacher, deleteTeacher, type TeacherData } from '@/api/teacher';

const router = useRouter();
const sname = ref('');
const tableData = ref<TeacherData[]>([]);
const total = ref(0);
const current = ref(1);
const size = ref(10);

const fetchData = async () => {
  try {
    const res = await getTeacherList({
      current: current.value,
      size: size.value,
      search: sname.value
    });
    tableData.value = res.data.records;
    total.value = res.data.total;
  } catch (error) {
    console.error('获取教师列表失败:', error);
    ElMessage.error('获取教师列表失败');
  }
};

const handleSearch = () => {
  current.value = 1;
  fetchData();
};

const handleReset = () => {
  sname.value = '';
  current.value = 1;
  fetchData();
};

const handleSizeChange = (val: number) => {
  size.value = val;
  fetchData();
};

const handleCurrentChange = (val: number) => {
  current.value = val;
  fetchData();
};

const goAdd = () => {
  router.push('/main/teacher/add');
};

const goEdit = (row: any) => {
  router.push(`/main/teacher/edit/${row.id}`);
};

const handleDelete = async (id: number) => {
  try {
    await ElMessageBox.confirm('确认删除该教师吗？', '提示', {
      type: 'warning'
    });
    await deleteTeacher(id);
    ElMessage.success('删除成功');
    fetchData();
  } catch (error) {
    if (error !== 'cancel') {
      console.error('删除教师失败:', error);
      ElMessage.error('删除教师失败');
    }
  }
};

onMounted(() => {
  fetchData();
});
</script>

<style scoped>
.header-title {
  font-size: 20px;
  font-weight: bold;
  margin-bottom: 16px;
}
.teacher-page {
  padding: 24px;
  background: #f7f8fa;
  min-height: 100vh;
}
.teacher-header {
  display: flex;
  align-items: center;
  margin-bottom: 16px;
}
.search-input {
  width: 300px;
  margin-right: 8px;
}
.add-btn {
  margin-left: auto;
}
.result-alert {
  margin-bottom: 16px;
}
.teacher-table {
  background: #fff;
  border-radius: 8px;
  overflow: hidden;
}
.el-link + .el-link {
  margin-left: 8px;
}
.pagination {
  margin-top: 16px;
  display: flex;
  justify-content: flex-end;
}
</style>
